package com.xzt.meeting.controller;

import com.xzt.meeting.entity.Article;
import com.xzt.meeting.entity.PageEntity;
import com.xzt.meeting.service.ArticleService;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

/**
 * @Author xzt
 * @Date 2022/5/8 18:11
 * @Version 1.0
 * @Introduce
 */

@WebServlet("/article/*")
public class ArticleServlet extends BaseServlet{

    private ArticleService articleService = new ArticleService();

    public void findPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Map<String,String> param = getParam(req);
        Integer currpage = Integer.parseInt(param.get("currPage"));
        String title = param.get("title");
        PageEntity<Article> pageData = articleService.findPage(currpage,title);

        req.setAttribute("pageData",pageData);

        req.setAttribute("title",title);

        req.getRequestDispatcher("/html/article.jsp").forward(req,resp);
    }

    public void addArt(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
        Article article = new Article();

        BeanUtils.populate(article, getParam(req));

        // 发布人信息
        article.setUserId(loginUser.getId());
        article.setPublishRealName(loginUser.getRealName());

        articleService.addArt(article);

        resp.sendRedirect("/article/findPage?currPage=1");
    }

    public void articleDetail(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Map<String, String> param = getParam(req);

        Long id = Long.parseLong(param.get("id"));

        // 浏览量+1
        articleService.updateBrowserCount(id);

        // 文章详情
        Article article = articleService.findById(id);

        // 收藏次数
        Integer favoriteCount = articleService.countFavoriteByArticleId(id);

        // 我是收藏过这篇文章
        Boolean isFavorite = articleService.isFavorite(loginUser.getId(), id);

        // 我关注的人里面也收藏过这篇文章的人
        List<Map<String, Object>> userFavorite = articleService.findUserFavorite(loginUser.getId(), id);

        req.setAttribute("article", article);
        req.setAttribute("isFavorite", isFavorite);
        req.setAttribute("userFavorite", userFavorite);
        req.setAttribute("favoriteCount", favoriteCount);

        req.getRequestDispatcher("/html/article_detail.jsp").forward(req, resp);
    }

    public void favorite(HttpServletRequest req, HttpServletResponse resp){
        Map<String, String> param = getParam(req);

        // 获取前端携带的文章id
        Long id = Long.parseLong(param.get("id"));

        // 判断当前登录的用户是否有收藏过这篇文章
        Boolean favorite = articleService.isFavorite(loginUser.getId(), id);

        if (favorite) {

            // 我已经收藏过这篇文章
            // 取消收藏
            articleService.unFavorite(loginUser.getId(), id);

            writerObjToString(resp, 0);
        } else {

            // 我没有收藏这个这篇文章
            articleService.favorite(loginUser.getId(), id);

            // 收藏
            writerObjToString(resp, 1);
        }
    }

    public void favoritePage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Map<String,String> param = getParam(req);
        Integer currpage = Integer.parseInt(param.get("currPage"));
        String title = param.get("title");
        PageEntity<Article> pageData = articleService.favoritePage(currpage,title,loginUser.getId());

        req.setAttribute("pageData",pageData);

        req.setAttribute("title",title);

        req.getRequestDispatcher("/html/article_collect.jsp").forward(req,resp);
    }
}
